# @Time    : 2018/5/2
# @Author  : ll & tgw
import sys
sys.path.append('service/gen-py')
from keraS import KerasService

from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket
from thrift.transport import TTransport
from operation.models.denseNet import denseNet
from operation.models.vGG import vGG
from operation.models.inception import inception
from operation.models.resNet import resNet
from operation.models.xception import xception
from operation.models.resNetV2 import resNetV2
from operation.models.resNeXt import resNeXt
from operation.models.mobileNet import mobileNet
from operation.models.nASNet import nASNet


class KerasServiceHandler:
    """
    rpc server: handle  the requests from clients and reply
    """

    def denseNet(self, input_data_path, outpu_data_path, model):
        return denseNet(input_data_path, outpu_data_path, model)

    def vGG(self, input_data_path, output_data_path, model):
        return vGG(input_data_path, output_data_path, model)

    def inception(self, input_data_path, output_data_path, model):
        return inception(input_data_path, output_data_path, model)

    def resNet(self, input_data_path, output_data_path, model):
        return resNet(input_data_path, output_data_path, model)

    def xception(self, input_data_path, output_data_path):
        return xception(input_data_path, output_data_path)

    def resNetV2(self, input_data_path, output_data_path, model):
        return resNetV2(input_data_path, output_data_path, model)

    def resNeXt(self, input_data_path, output_data_path, model):
        return resNeXt(input_data_path, output_data_path, model)

    def mobileNet(self, input_data_path, output_data_path, model):
        return mobileNet(input_data_path, output_data_path, model)

    def nASNet(self, input_data_path, output_data_path, model):
        return nASNet(input_data_path, output_data_path, model)


# create the server
handler = KerasServiceHandler()
processor = KerasService.Processor(handler)
# listen the port
transport = TSocket.TServerSocket("localhost", 9090)
# select transportation layer
tfactory = TTransport.TBufferedTransportFactory()
# select transportation protocol
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# create the server
# TODO:we should not use a simple server here
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print("Starting Keras thrift server in python...")
server.serve()
print("done!")
